#include "widget.h"
#include "ui_widget.h"
#include <QProgressBar>


void QProgressBarTest(Ui::Widget* ui, Widget* Pthis, QTimer* timer) {
    ui->progressBar->setValue(0);

    QObject::connect(timer, &QTimer::timeout, Pthis, &Widget::handle);

    //别忘了启动定时器。启动操作要在connect之后
    timer->start(100);
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置进度条样式
    ui->progressBar->setStyleSheet("QProgressBar {"
                                   "    text-align: center;"  // 文字居中
                                   "    color: black;"        // 文字颜色
                                   "    border: 2px solid grey;"   // 边框设置
                                   "    border-radius: 5px;"   // 边框圆角
                                   "    background: white;"    // 背景颜色
                                   "}"
                                   "QProgressBar::chunk {"
                                   "    background-color: red;"  // 进度条颜色
                                   "    width: 20px;" // 进度块的宽度
                                   "}");
    timer = new QTimer(this);
    QProgressBarTest(ui, this, timer);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
   //获取到进度条的当前数值
   int value = ui->progressBar->value();
   if (value >= 100) {
       //进度条满了,就可以停止定时器
       timer->stop();
       return;
   }
   ui->progressBar->setValue(value + 1);
}

